<template>
	<view class="container">
		<view class="wrapper">
			<view class="input-content">
				<view class="cu-form-group"
					style="border: 2upx solid whitesmoke;margin-bottom: 20px;border-radius: 30px">
					<view class="title">手机号</view>
					<input type="number" v-model="userLoginVo.telephone" placeholder="请输入手机号" maxlength="11"
						data-key="telephone" @input="inputChange" />
				</view>
				<view class="cu-form-group"
					style="border: 2upx solid whitesmoke;margin-bottom: 20px;border-radius: 30px">
					<text class="title">验证码</text>
					<input type="number" v-model="userLoginVo.captcha" placeholder="请输入验证码" maxlength="6"
						data-key="captcha" @input="inputChange" @confirm="toLogin" />
					<button class="send-msg" @click="sendCaptcha" :disabled="sending">{{sendTime}}</button>
				</view>
				<view class="cu-form-group"
					style="border: 2upx solid whitesmoke;margin-bottom: 20px;border-radius: 30px">
					<text class="title">设置密码</text>
					<input type="password" v-model="userLoginVo.password" placeholder="请设置新密码"
						placeholder-class="input-empty" maxlength="20" minlength="6" data-key="password"
						@input="inputChange" @confirm="toLogin" />
				</view>
			</view>
			<button class="confirm-btn" @click="toLogin">立即找回</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userLoginVo: {
					telephone: '',
					captcha: '',
					password: ''
				},
				sending: false,
				sendTime: '获取验证码',
				count: 60,
				logining: false
			}
		},

		methods: {
			sendCaptcha() {
				if (!this.userLoginVo.telephone) {
					uni.showToast({
						title: '请输入手机号',
						icon: "error"
					});
				} else if (this.userLoginVo.telephone.length != 11) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: "error"
					});
				} else {
					this.$myRequest({
						url: '/sendCaptcha',
						data: {
							telephone: this.userLoginVo.telephone
						}
					}).then(r => {
						if (r.data.code == 200) {
							console.log(r);
							this.countDown();
						}
					})
				}
			},
			countDown() {
				const {
					count
				} = this;
				if (count === 1) {
					this.count = 60;
					this.sending = false;
					this.sendTime = '获取验证码'
				} else {
					this.count = count - 1;
					this.sending = true;
					this.sendTime = count - 1 + '秒后重新获取';
					setTimeout(this.countDown.bind(this), 1000);
				}
			},
			inputChange(e) {
				const key = e.currentTarget.dataset.key;
				this[key] = e.detail.value;
			},
			navBack() {
				uni.navigateBack();
			},


			navTo(url) {
				uni.navigateTo({
					url
				})
			},
			toLogin() {
				if (!this.userLoginVo.telephone) {
					uni.showToast({
						title: '请输入手机号',
						icon: "error"
					});
				} else if (this.userLoginVo.telephone.length != 11) {
					uni.showToast({
						title: '请输入正确的手机号',
						icon: "error"
					});
				} else if (!this.userLoginVo.captcha) {
					uni.showToast({
						title: '请输入验证码',
						icon: "error"
					});
				} else if (!this.userLoginVo.password) {
					uni.showToast({
						title: '请输入密码',
						icon: "error"
					});
				} else {
					this.$myRequest({
						url: '/login/forgetPwd',
						data: this.userLoginVo,
						method: 'post'
					}).then(r => {
						if (r.data.code == 200) {
							// 将用户id存储在本地缓存中
							uni.showToast({
								title: "找回成功，即将返回登陆",
								icon: "success"
							})
							// 返回登录页面
							setTimeout(function() {
								uni.navigateTo({
									url:'/pages/public/login'
								})
							}, 1500)
						}
					});
				}
			}
		}
	}
</script>

<style lang='scss'>
	.send-msg {
		border-radius: 30px;
		color: white;
		height: 30px;
		font-size: 14px;
		line-height: 30px;
		background: #5E81F9;
	}

	.container {
		padding-top: 32upx;
		position: relative;
		width: 100%;
		height: 100%;
		overflow: hidden;
		background: #111224;
	}

	.wrapper {
		position: relative;
		z-index: 90;
		background: #111224;
		padding-bottom: 20px;
	}


	.input-content {
		padding: 32upx 80upx;
	}


	.confirm-btn {
		width: 600upx;
		height: 80upx;
		line-height: 80upx;
		border-radius: 60upx;
		margin-top: 32upx;
		background: #5E81F9;
		color: #fff;
		font-size: 32upx;

		&:after {
			border-radius: 60px;
		}
	}
</style>
